package com.weijx.dishes.controller;

import com.weijx.dishes.utils.ReturnResult;
import com.weijx.dishes.entity.Category;
import com.weijx.dishes.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * com.weijx.dishes.controller.CategoryController
 * 描述：
 * on 2025/10/24-下午3:59
 *
 * @author weijx
 */


@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 获取所有分类
     */
    @GetMapping
    public ReturnResult<List<Category>> getAllCategories() {
        try {
            List<Category> categories = categoryService.getAllCategories();
            return ReturnResult.success("获取分类成功", categories);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取分类失败: " + e.getMessage(), null);
        }
    }

    /**
     * 根据父级ID获取分类
     */
    @GetMapping("/parent/{parentId}")
    public ReturnResult<List<Category>> getCategoriesByParentId(@PathVariable String parentId) {
        try {
            List<Category> categories = categoryService.getCategoriesByParentId(parentId);
            return ReturnResult.success("获取分类成功", categories);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取分类失败: " + e.getMessage(), null);
        }
    }

    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    public ReturnResult<Category> getCategoryById(@PathVariable String id) {
        try {
            Category category = categoryService.getCategoryById(id);
            if (category != null) {
                return ReturnResult.success("获取分类成功", category);
            } else {
                return ReturnResult.error(404, "分类不存在", null);
            }
        } catch (Exception e) {
            return ReturnResult.error(500, "获取分类失败: " + e.getMessage(), null);
        }
    }
}
